用Python制作不和谐机器人,但它不起作用



我想为我和我的朋友做一个不和聊天机器人来使用它。我用python来做。我的问题是有3个命令机器人应该做。但是只有1个有效或者当我改变代码的位置时只有2个有效。请帮助

代码:

import os
import random
import json
import discord
from dotenv import load_dotenv
import requests


client = discord.Client()
@client.event
async def on_ready():
print(f'{client.user.name} has connected to Discord!')
sad_words = ["sad", "owo cry", "unhappy", "a", "miserable"]
starter_encouragements = [
"Cheer up!",
"Hang in there.",
"You are a great person / bot!",
"i love you",
"what happend",
"dont be sad",
"if you are sad i´m sad"
]
def get_quote():
response = requests.get("https://zenquotes.io/api/random")
json_data = json.loads(response.text)
quote = json_data[0]['q'] + " -" + json_data[0]['a']
return(quote)

@client.event
async def on_message(message):
if message.author == client.user:
return
msg = message.content
if msg.startswith('$inspire'):
quote = get_quote()
await message.channel.send(quote)

if any(word in msg for word in sad_words):
await message.channel.send(random.choice(starter_encouragements))

@client.event
async def on_message(message):
if message.author == client.user:
return
cry_gif = [
'https://tenor.com/view/crying-anime-cry-gif-13668435',
'https://c.tenor.com/OhuSWqAsQH4AAAAM/anime-girl-sad-sad.gif',

'https://c.tenor.com/bMSZQ4j3CQkAAAAM/anime-cry.gif ',
'https://c.tenor.com/zfmbNj3IpAgAAAAC/sorry-crying.gif'

]
if message.content == '$hero cry':
response = random.choice(cry_gif)
await message.channel.send(response)



client.run(os.getenv("abc"))

当$激励和"伤心的话"工作其他命令$hero cry不起作用。当$哭的时候,其他的都不工作。

您可以有多个on_message,但这真的很愚蠢,所以最好将整个东西与if/elif/else语句组合在一起,如Łukasz Kwieciński所说。另外,Free Code Camp的discord.py教程也不是很好。没有很好的discord.py教程,所以开始阅读吧。

你也可以到官方的discord.py服务器中寻求帮助

还有一件事,discord.py已经正式结束了,没有人再维护这个库了,原来的维护者已经辞职了,没有人想要维护这个库

你的代码不能正常工作,因为你已经声明了两个函数,但具有相同的名称,这是无效的,你不必做两个单独的"on_message"函数,你可以使用另一个if语句,并加上"$hero "one_answers"inspire&quot美元;在同一个on_message函数

This Will Work:

import os
import random
import json
import discord
from dotenv import load_dotenv
import requests
client = discord.Client()

@client.event
async def on_ready():
print(f'{client.user.name} has connected to Discord!')
sad_words = ["sad", "owo cry", "unhappy", "a", "miserable"]
starter_encouragements = ["Cheer up!", "Hang in there.", "You are a great person / bot!",
"i love you", "what happend", "dont be sad", "if you are sad i'm sad"]
cry_gif = [
'https://tenor.com/view/crying-anime-cry-gif-13668435',
'https://c.tenor.com/OhuSWqAsQH4AAAAM/anime-girl-sad-sad.gif',
'https://c.tenor.com/bMSZQ4j3CQkAAAAM/anime-cry.gif ',
'https://c.tenor.com/zfmbNj3IpAgAAAAC/sorry-crying.gif'
]
def get_quote():
response = requests.get("https://zenquotes.io/api/random")
json_data = json.loads(response.text)
quote = json_data[0]['q'] + " -" + json_data[0]['a']
return(quote)

@client.event
async def on_message(message):
if message.author == client.user:
return
msg = message.content
if msg.startswith('$inspire'):
quote = get_quote()
await message.channel.send(quote)
if any(word in msg for word in sad_words):
await message.channel.send(random.choice(starter_encouragements))
if message.content == '$hero cry':
response = random.choice(cry_gif)
await message.channel.send(response)
client.run(os.getenv("abc"))

最新更新