我正在尝试在discord中扮演一个全新的角色(使用discord.py),但是我尝试的所有方法都无法正常工作。我已经尝试过
await guild.create_role(name("R3KT")) #NameError: name 'guild' not defined
和
author = ctx.message.author
await client.create_role(author.server, name="role name") #NameError: name 'ctx' not defined
我尝试将ctx和guild更改为“客户端”,但仍然无法正常工作。如果需要,我将发送完整的代码(不包含不一致的名称和bot密钥)
这是完整的代码:
import discord
token = "bot-key"
client = discord.Client()
@client.event
async def on_ready():
print("Bot is online and connected to Discord")
@client.event
async def on_message(message):
message.content = message.content.upper()
if message.author == client.user:
return
if message.content.startswith("AS HELLO"):
await message.channel.send("works")
elif message.content.startswith("AS CREATEROLE"):
if str(message.author) == "myDiscName#6969":
author = message.author
await discord.create_role(author.server, name="R3KT")
elif message.content.startswith("AS GIVEROLE"):
if str(message.author) == "myDiscName#6969":
print("give role")
client.run(token)
答案 0 :(得分:0)
在您提供的第二个示例中,您似乎在旧d.py(0.16.x
)文档和d.py rewrite(1.x
)之间使用了混搭。
请确保您已安装最新版本(重写),因为不再维护异步。
下面是命令装饰器的一个示例(用法:!newrole Member
)
@client.command()
async def newrole(ctx, *, rolename=None):
if not rolename:
await ctx.send("You forgot to provide a name!")
else:
role = await ctx.guild.create_role(name=rolename, mentionable=True)
await ctx.author.add_roles(role)
await ctx.send(f"Successfully created and assigned {role.mention}!")
mentionable
kwarg不是强制性的-如果未指定,则默认为False
-在本示例中,我将其设置为True
。您还可以为角色编写自己的权限。
另一个使用on_message
的示例-建议改用命令装饰器,因为args更易于处理
async def on_message(message):
args = message.content.split(" ")[2:] # 2: because prefix contains space
if message.content.lower().startswith("as createrole"):
role = await message.guild.create_role(name=" ".join(args))
await message.author.add_roles(role)
await message.channel.send(f"Successfully created and assigned {role.mention}!")
参考:
答案 1 :(得分:0)
您不想按姓名和电话号码进行检查(因为您可以更改为硝基),因此应该按ID进行检查,该ID对每个用户都是唯一的并且是不变的。
还要注意,server
在guild
中被称为discord.py
。
elif message.content.startswith("AS CREATEROLE"):
if message.author.id == YOUR_ID_GOES_HERE: # find your id by right clicking on yourself and selecting "copy id" at the bottom of the menu of options
author = message.author
# create a role named "R3KT" that has the permissions "Administrator" that has the color "white" that is not displayed separately and is not mentionable by everyone
await message.guild.create_role(name="R3KT", permissions=discord.Permissions(8), colour=discord.Colour.from_rgb(255, 255, 255), hoist=False, mentionable=False)
Permissions integer calculator
赋予自己角色:
elif message.content.startswith("AS GIVEROLE"):
if message.author.id == YOUR_ID_GOES_HERE:
user = message.author
await user.add_roles(message.guild.get_role(ROLE_ID_GOES_HERE), reason='Someone did a giverole bot command') # left click on the role and click "copy id"