简体   繁体   English

用 Python 制作 Discord Bot 但它不起作用

[英]Making Discord Bot with Python but it doesn´t work

I´m trying to do a discord bot for me and my friends to use it.I´m using python to do it.我正在尝试为我和我的朋友们做一个不和谐的机器人来使用它。我正在使用 python 来做这件事。 My problem is there is 3 commands bot should do.我的问题是机器人应该执行 3 个命令。 But only 1 works or when i change the places of codes only 2 of them works.但是只有 1 个有效,或者当我更改代码的位置时,只有 2 个有效。 Please help请帮忙

The Code:编码:

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"))

When $inspire and "sad words" work other command $hero cry wont work.当 $inspire 和 "sad words" 起作用时,其他命令 $hero cry 将不起作用。 And when $cry work the others wont work.当 $cry 工作时,其他人将无法工作。

You can have more than one on_message but thats jut really dumb, so its better to just combine the whole thing together with if/elif/else statements like Łukasz Kwieciński said.您可以拥有多个 on_message 但这确实很愚蠢,因此最好将整个内容与 Łukasz Kwieciński 所说的 if/elif/else 语句结合在一起。 Also Free Code Camp's discord.py tutorial isn't that good. Free Code Camp 的 discord.py 教程也不是很好。 There is no good discord.py tutorial, so get reading.没有好的 discord.py 教程,所以开始阅读吧。

You can also go ask for help in the Official discord.py server你也可以去官方 discord.py 服务器寻求帮助

Another thing, discord.py is officially over, there is no-one maintaning the library anymore, the original maintainer has stepped down and there is no-one that wants to maintain the library还有一件事,discord.py 正式结束了,没有人维护库了,原来的维护者已经下台了,没有人想维护库

Your code's not working properly because you have declared two functions but with same name which is not valid, you don't have to make two separate "on_message" functions for different commands, you can just use another if statement and put your "$hero cry" and "$inspire" in same on_message function您的代码无法正常工作,因为您声明了两个函数但名称相同但无效,您不必为不同的命令创建两个单独的“on_message”函数,您可以使用另一个 if 语句并将您的“$ hero在同一个 on_message 函数中哭泣”和“$inspire”

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"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM