我正在尝试使用discord.py 编写一个discord 机器人。我想创建一个discord.client 类的子类,这样我就可以向该类添加一些属性。不幸的是我一直收到这个错误:
回溯(最近一次调用最后一次): 文件“C:/Users/laser/PycharmProjects/DiscordNut/main.py”,第 5 行,位于 机器人类(客户端): 类型错误: module.init() 最多接受 2 个参数(给定 3 个)
这是我的代码:
from discord import client
import random as rand
class Bot(client):
pass
async def roll(self, *args):
input = args[0]
target = input.find('d')
numDice = int(input[:target])
diceSize = int(input[target + 1:])
output = 0
for i in range(numDice):
output += rand.randint(1, diceSize)
await self.send_message(self.get_channel("505154560726269953"), "You rolled a: " + str(output))
def parse(content, delimeter):
target = content.find(delimeter)
if content.find("\"") == 0:
content = content[1:]
target = content.find("\"")
if target == 0:
return parse(content[1:], delimeter)
elif target < 0:
return [content]
else:
parsed = []
parsed.append(content[:target])
return parsed + parse(content[target + 1:], delimeter)
client = Bot()
@client.event
async def on_ready():
client.users = [i for i in client.get_all_memebers()]
print(client.users)
@client.event
async def on_message(message):
parsed = None
if message.startswith('!'):
parsed = parse(message.content[1:], " ")
if parsed[0] in client.commands:
args = [i for i in parsed if i != parsed[0]]
client.commands[parsed[0]](args)
client.run("<TOKEN>")
您可以通过继承来做到这一点
commands.Bot
from discord.ext import commands
class MyBot(commands.Bot):
"""
you can still access all attributes of commands.Bot
"""
# create the bot instance
bot = MyBot(
# still able to access the same attributes
command_prefix = '',
intents = intents
)
discord.py 文档给出了
commands.Bot
比使用
Client
更好的原因
该类是discord.Client 的子类,因此您可以使用discord.Client 执行的任何操作都可以使用此机器人执行。