简体   繁体   中英

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. My problem is there is 3 commands bot should do. But only 1 works or when i change the places of codes only 2 of them works. 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. And when $cry work the others wont work.

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. Also Free Code Camp's discord.py tutorial isn't that good. There is no good discord.py tutorial, so get reading.

You can also go ask for help in the Official discord.py server

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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