我在制作不和谐机器人方面还是个新手。我已经创建了一个方法来获取机器人所连接到的服务器中所有成员的列表。这种方法可以很好地工作。但是,我也在尝试让机器人接受命令。我在这方面遇到了很多麻烦,我一直认为我应该使用commands.Bot而不是客户端。不过,我不知道如何让我的原始方法与commands.Bot一起工作。任何帮助都将不胜感激!
import os
import discord
import csv
from collections import defaultdict
from discord.ext import commands
intents = discord.Intents.all()
client = discord.Client(intents=intents)
outP = open("giveawayList.txt", "w")
bot = commands.Bot(command_prefix='!')
@client.event
async def on_message(message):
if message == "!test":
await message.channel.send("you failed the test")
@client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break
print(
f'{client.user} is connected to the following guild: \n'
f'{guild.name} (id: {guild.id})'
)
count = 0
for guild in client.guilds:
for member in guild.members:
print(member)
if 'Bots' not in str(member.roles):
print(member.name, ' ')
outP.write(member.name)
outP.write("\n")
count += 1
print('Number of members: ' + str(count))发布于 2021-01-23 05:36:31
最好使用commands.bot而不是Client,因为它是一个扩展版本,因为它继承了Client的所有功能。
我看到您已经尝试从使用Client迁移到使用commands.bot,但请记住以下几点:
您不希望同时使用这两个工具,因此这是错误的:
client = discord.Client(intents=intents)
bot = commands.Bot(command_prefix='!')你应该只保留机器人的那个。
除此之外,你还需要替换装饰器中的client和函数内部的调用。更正后的代码应该如下所示:
import os
import discord
import csv
from collections import defaultdict
from discord.ext import commands
intents = discord.Intents.all()
outP = open("giveawayList.txt", "w")
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_message(message):
if message == "!test":
await message.channel.send("you failed the test")
await bot.process_commands(message)
@bot.event
async def on_ready():
for guild in bot.guilds:
if guild.name == GUILD:
break
print(
f'{bot.user} is connected to the following guild: \n'
f'{guild.name} (id: {guild.id})'
)
count = 0
for guild in bot.guilds:
for member in guild.members:
print(member)
if 'Bots' not in str(member.roles):
print(member.name, ' ')
outP.write(member.name)
outP.write("\n")
count += 1
print('Number of members: ' + str(count))发布于 2021-01-23 05:35:06
所以,关于你的问题,我将逐一回答:
1-客户端和机器人没有任何不同,这意味着你在两者上使用相同的功能应该不会有任何问题(两者都不是比对方更好,这意味着两者都会做同样的事情)
2-现在,要让你的机器人接收/接受命令,你可以使用await函数,它基本上是等待命令被触发,并给出输出。
例如:
@Client.command()
async def botping(ctx):
await ctx.send(f"The bot's ping is {round(Client.latency*1000)} ms")此处的命令(例如):
我们定义了一个名为"botping“的命令,我们传递的ctx是机器人正在编写的上下文,当你在聊天中键入"prefix"botping时,你拥有的await将触发命令。
希望本文能帮助您理解接受命令的概念,并且client和bot之间没有区别
有关更多信息,我建议您查看discord.py文档:
https://stackoverflow.com/questions/65852705
复制相似问题