Python 3.6 Discord bot Bot事件冲突
Python 3.6 Discord bot Bot events conflict
所以我有两个机器人事件
用 "k" 回复消息 "k" 的人
一个简单的猜测我的数字在 1-10 之间
问题是它们发生冲突并且只有一个有效(下面的那个)IDK 我所缺少的。代码:
@client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
if message.author.bot: return
if message.content==('k'):
msg = 'k'.format(message)
await client.send_message(message.channel, msg)
await client.process_commands(message)
@client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
if message.content.startswith('!guess'):
await client.send_message(message.channel, 'Guess a number between 1 to 10')
def guess_check(m):
return m.content.isdigit()
guess = await client.wait_for_message(timeout=10.0, author=message.author, check=guess_check)
answer = random.randint(1, 10)
if guess is None:
fmt = 'Sorry, you took too long. It was {}.'
await client.send_message(message.channel, fmt.format(answer))
return
if int(guess.content) == answer:
await client.send_message(message.channel, 'You are right!')
else:
await client.send_message(message.channel, 'Sorry. It is actually {}.'.format(answer))
await client.process_commands(message)
那么我该怎么做才能使它们不冲突?
您已经定义函数 on_message()
两次。
为了演示这个问题,如果我 运行 以下代码,您期望输出是什么?
def f(x):
print(x)
def f(x):
print('Nothing useful')
f(3)
您的代码中存在同样的问题。
假设 discord 框架将在收到消息时调用一个名为 on_message()
的函数,您需要有 one on_message()
函数来处理 任意 输入。因此,它看起来像这样:
@client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
if message.content==('k'):
...
if message.content.startswith('!guess'):
...
如果您觉得特别时髦,可以将 if
块的内容分解成它们自己的函数,以使脚本更易于阅读,但我会将其作为练习留给你,一旦你完成了剩下的工作。
所以我有两个机器人事件 用 "k" 回复消息 "k" 的人 一个简单的猜测我的数字在 1-10 之间 问题是它们发生冲突并且只有一个有效(下面的那个)IDK 我所缺少的。代码:
@client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
if message.author.bot: return
if message.content==('k'):
msg = 'k'.format(message)
await client.send_message(message.channel, msg)
await client.process_commands(message)
@client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
if message.content.startswith('!guess'):
await client.send_message(message.channel, 'Guess a number between 1 to 10')
def guess_check(m):
return m.content.isdigit()
guess = await client.wait_for_message(timeout=10.0, author=message.author, check=guess_check)
answer = random.randint(1, 10)
if guess is None:
fmt = 'Sorry, you took too long. It was {}.'
await client.send_message(message.channel, fmt.format(answer))
return
if int(guess.content) == answer:
await client.send_message(message.channel, 'You are right!')
else:
await client.send_message(message.channel, 'Sorry. It is actually {}.'.format(answer))
await client.process_commands(message)
那么我该怎么做才能使它们不冲突?
您已经定义函数 on_message()
两次。
为了演示这个问题,如果我 运行 以下代码,您期望输出是什么?
def f(x):
print(x)
def f(x):
print('Nothing useful')
f(3)
您的代码中存在同样的问题。
假设 discord 框架将在收到消息时调用一个名为 on_message()
的函数,您需要有 one on_message()
函数来处理 任意 输入。因此,它看起来像这样:
@client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
if message.content==('k'):
...
if message.content.startswith('!guess'):
...
如果您觉得特别时髦,可以将 if
块的内容分解成它们自己的函数,以使脚本更易于阅读,但我会将其作为练习留给你,一旦你完成了剩下的工作。