如何使我的Python Discord机器人在加入时扮演角色? [英] How do I make my Python discord bot give roles on join?

查看:56
本文介绍了如何使我的Python Discord机器人在加入时扮演角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个机器人正在尝试做得更好.它被称为主持人机器人,我正在努力添加新命令.我只是在任何地方都找不到代码.

I have a bot that I'm trying to make better. It's called moderator bot and I'm working on adding new commands. I just can't find the code anywhere.

当用户加入服务器时,如何让主持人bot赋予用户角色.以及如何使它可由服务器所有者配置,以便该机器人可以在不同的服务器上使用?

How do I get moderator bot to give user roles when they join the server. And how do I make it configurable by the server owner so that the bot can be used over different servers?

我还希望它向所有者命令发送DM,以便他们可以看到它以及只有所有者设置的角色能够访问该命令.

I also want it to DM the owner commands so they can see it along with only owner-set roles being able to access that command.

这听起来很先进,也许是,但是外面有人可以为我编写代码或告诉我如何制作代码吗?

It sounds advanced and probably is, but can someone out there write the code for me or tell me how to make that?

推荐答案

1)要使漫游器在成员加入时为其赋予角色,您需要具有 add_roles()在您的 on_member_join 事件中.这可以通过

1) To make the bot give a member a role when they join, you need to have add_roles() in your on_member_join event. This can be accomplished with

@bot.event
async def on_member_join(member):
    role = discord.utils.get(member.server.roles, id="<role ID>")
    await bot.add_roles(member, role)

2)要为每台服务器自定义角色,您需要一些文件来保存服务器ID,并在用户加入时指定角色ID(我个人将使用 .db 文件,然后使用sqlite3从python对其进行编辑,但您可以执行任何操作).您还需要稍微编辑我给出的 on_member_join 示例,以根据文件中的 member.server.id 选择角色ID,并在 discord.utils.get()

2) To have the role be customized for each server, you would need some file to hold the server id, and the role id to be given when the user joins (I personally would use a .db file, and sqlite3 to edit it from python, but you can do whatever you want). You would also need to slightly edit the on_member_join example I gave to select the role id based on the member.server.id from the file, and use the role id in discord.utils.get()

3)要使机器人DM 任何人为默认帮助消息,请将 pm_help = True 添加到您的 Bot()参数.要让普通用户将部分命令并全部发送给所有者,您将需要创建一个新的帮助命令.为此,您需要在代码顶部附近添加 bot.remove_command('help'),然后创建一个名为help的命令.然后,要仅将命令发送给所有者,请添加

3) To have the bot DM anyone the default help messages, add pm_help=True to your Bot() parameters. To have part of your commands the average user and all sent to only the owner, you will need to make a new help command. To do this, you will need to add bot.remove_command('help') near the top of your code, and then create a command called help. Then, to have commands only sent to the owner, add

if ctx.message.author.id == ctx.message.server.owner.id:
    await bot.send_message(ctx.message.author, <help message>)

转到您的帮助命令.

希望这会有所帮助

这篇关于如何使我的Python Discord机器人在加入时扮演角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆