#python #api #discord.py
#питон #API #discord.py
Вопрос:
Я добавил 2/3 API для бота в сообщение. Следующие коды: Numbers API
def random_math(): response = requests.get("http://numbersapi.com/random/math") json_data = json.loads(response.text) math = json_data[0]["n"] " -" json_data[0]["a"] return(math)
if msg.startswith("$math"): math = random_math() await message.channel.send(math)
API для шуток
def joke_api(): response = requests.get("https://api.icndb.com/jokes/random") json_data = json.loads(response.text) joke = json_data [0]["j"] " -" json_data [0]["a"] return(joke)
if msg.startswith("$joke"): joke = joke_api() await message.channel.send(joke)
И вот следующие коды в main.py
import discord import os import requests import json import random from replit import db from keep_alive import keep_alive client = discord.Client() sad_words = ["sad", "depressed", "unhappy", "angry", "miserable", "bad", "lost", "weak", "depressing", "sucks", "hurts", " i suck"] starter_encouragements = [ "Cheer up!", "Hang in there.", "You are a great person!", "You can do it!", "You are Amazing!", "You are the Best Gamer in the World!", "You are the Best!" ] if "responding" not in db.keys(): db["responding"] = True 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) def random_math(): response = requests.get("http://numbersapi.com/random/math") json_data = json.loads(response.text) math = json_data[0]["n"] " -" json_data[0]["a"] return(math) def joke_api(): response = requests.get("https://api.icndb.com/jokes/random") json_data = json.loads(response.text) joke = json_data [0]["j"] " -" json_data [0]["a"] return(joke) def update_encouragements(encouraging_message): if "encouragements" in db.keys(): encouragements = db["encouragements"] encouragements.append(encouraging_message) db["encouragements"] = encouragements else: db["encouragements"] = [encouraging_message] def delete_encouragment(index): encouragements = db["encouragements"] if len(encouragements) gt; index: del encouragements[index] db["encouragements"] = encouragements @client.event async def on_ready(): print("Bot logged in to discord as {0.user}".format(client)) @client.event async def on_message(message): if message.author == client.user: return msg = message.content if msg.startswith("$quote"): quote = get_quote() await message.channel.send(quote) if msg.startswith("$math") math = random_math() await message.channel.send(math) if msg.startswith("$joke") joke = joke_api() await message.channel.send(joke) if db["responding"]: options = starter_encouragements if "encouragements" in db.keys(): options = options db["encouragements"] if any(word in msg for word in sad_words): await message.channel.send(random.choice(options)) if msg.startswith("new_msg"): encouraging_message = msg.split("$new ",1)[1] update_encouragements(encouraging_message) await message.channel.send("New encouraging message added.") if msg.startswith("del_msg"): encouragements = [] if "encouragements" in db.keys(): index = int(msg.split("$del",1)[1]) delete_encouragment(index) encouragements = db["encouragements"] await message.channel.send(encouragements) if msg.startswith("list"): encouragements = [] if "encouragements" in db.keys(): encouragements = db["encouragements"] await message.channel.send(encouragements) if msg.startswith("responding"): value = msg.split("responding ",1)[1] if value.lower() == "true": db["responding"] = True await message.channel.send("Responding is on.") else: db["responding"] = False await message.channel.send("Responding is off.") keep_alive() client.run(os.getenv("TOKEN"))
Когда я отправляю сообщение $quote
, оно отвечает цитатой из Zenquotes API. Но когда я отправляю сообщение $math
и $joke
, бот не отвечает на него.
Комментарии:
1. Не похоже, чтобы вы когда-либо снимали «$» с сообщения. Так что это не будет совпадать
msg.startswith("joke")
, потому что на самом деле все начинается с"$joke"