我正在尝试创建一个discord机器人,该机器人创建一个类别并设置角色和用户的权限,并以用户id作为参数:
for x in range(2, len(args)):
member= client.get_user(int(args[x]))
await message.guild.categories[-1].set_permissions(member, send_messages = True)
其中args是包含命令,类别名称和用户ID的字符串数组。 用法应该是:
!create category-name 0000000 1111111
我对此有一些问题,因为就像机器人看不到服务器的成员,并且他可以添加的唯一用户是我,如果在第一个参数中指定服务器的所有者(我的000000例)。 如果我输入别人的ID,则漫游器不会在该类别中为该用户添加权限。 我发现也许该机器人看不到其他用户,事实上,如果我把这一行放在那儿:
print(message.guild.members)
它将仅将机器人作为成员打印,
打印以下内容:[<成员ID = 762749337700007946 name ='RoomBzot'discriminator ='1334'bot = True nick = None guild =>]
我不知道它为什么要计入会员但看不到其他用户的原因,我该怎么办?
答案 0 :(得分:0)
安装该库的1.4.2版:pip install -U discord.py==1.4.2
答案 1 :(得分:0)
由于derw链接的答案不再可用;
正如OP所指出的,我设法通过实例化discord bot客户端来解决此问题:
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
此外,我还必须在Discord开发人员门户的bot设置下启用“ SERVER MEMBERS INTENT”特权网关意图:
答案 2 :(得分:0)
你不需要像那样拆分它,我认为你不知道,但你真的应该检查来自 here 的命令,为 async def 函数指定你的参数,然后你可以做类似的事情这个:
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
token = "YourBotsTokenHere"
bot = commands.Bot(command_prefix='!', intents=intents) # Or whatever prefix you want
@bot.event
async def on_ready():
print(f"Bot {bot.user} has connected to Discord!")
@bot.command()
async def create(ctx, type_, member: discord.Member, categoryName=None, *, name):
# member is going to return a member object so u can do stuff like member.id or member.name, etc...
if type_.lower() == "category":
# Code to create a category with the name variable
# some pseudo-code that might look similar to what u need
role = get(member.server.roles, name="NameOfTheRole")
await bot.add_roles(member, role)
# insert a part of ur own code here for category thingy
elif type_.lower() == "channel":
# Code to create a channel with the name variable, category name must be also specified so we can check to what category the user is reffering to, also check if the user has the role to create that channel
if categoryName == None: return
catgName = categoryName.replace('-', ' ') # Replaces "-" with an empty space
bot.run(token)
至于其他东西,启用 members
意图工作正常,我已经在示例中编写了