#python #discord #discord.py #discord.py-rewrite
#python #Discord #discord.py
Вопрос:
контекст: я сделал недавно, и, возможно, после некоторых запусков он внезапно не отправляет сообщение и не возвращает синтаксическую ошибку, вот код
import discord
from discord import message
from discord.ext import commands
import random
import math
import requests
import wikipedia
from PyDictionary import PyDictionary
from googletrans import Translator
trans= Translator()
dic= PyDictionary()
client= commands.Bot(command_prefix='!')
@client.event
async def on_ready():
await client.change_presence(activity=discord.Game("Shellshockers"))
print("Works")
@client.event
async def on_member_join(member, ctx):
print(f'{member} has joined')
came= [" Has entered this server", " Came, hope you brought some pizza", "came", f"Hello there {member}, welcome to the shellshockers server"]
welcmessage= random.choice(came)
await ctx.send(f"{member} {welcmessage}ms")
@client.event
async def on_member_remove(message,member):
left= [" Left the server", " Sadly left, we did not egg-spect this!", "left unfortunetly"]
leftmessage= random.choice(left)
await message.channel.send(f"{member}{leftmessage}")
print(f'{member} has left server')
@client.command()
async def ping(ctx):
await ctx.send(f"Ping is{round(client.latency*1000)}ms")
@client.command()
async def map(ctx):
maps= ["Road","Field","Shellville","Castle","Dirt","Feedlot","Moonbase","Blue","Two Towers","Ruins"]
map2= random.choice(maps)
await ctx.send(f"Play in {map2}")
@client.command()
async def mode(ctx):
modes=["Captula the Spatula","Teams","Free For All"]
mode2= random.choice(modes)
await ctx.send(f"Play in {mode2} mode")
@client.command(aliases=["all","choose all","choose mode and map"])
async def random_all(ctx):
maps1= ["Road","Field","Shellville","Castle","Dirt","Feedlot","Moonbase","Blue","Two Towers","Ruins"]
modes1=["Captula the Spatula","Teams","Free For All"]
map1= random.choice(maps1)
mode1= random.choice(modes1)
await ctx.send(f"In {map1} play in {mode1} mode")
@client.command()
async def coinflip(ctx):
coinside= ["Heads","Tails"]
coinpicked= random.choice(coinside)
await ctx.send(f"The coin has fliped to {coinpicked}")
@client.command()
async def purge(ctx, amount=6):
await ctx.channel.purge(limit=amount)
await ctx.send("Done!")
@client.command()
async def kick(ctx, member : discord.Member, *, reason=None):
await member.kick(reason=reason)
await ctx.send(f"{member} has been kicked")
@client.command()
async def ban(ctx, member : discord.Member, *, reason=None):
await member.ban(reason=reason)
await ctx.send(f"{member} has been banned")
@client.command()
async def Helper(ctx):
info= "!map= Chooses a random map n !mode= chooses random mode n !all= Chooses random mode and map n !coinflip= Flips a coin "
await ctx.send(info)
@client.event
async def command_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("please pass all requirements")
@client.command()
async def wiki(ctx,*, topic):
final= (wikipedia.summary(topic, sentences= 5))
await ctx.send(final)
@client.command()
async def w(ctx, city):
url='http://api.openweathermap.org/data/2.5/weather?q={}amp;appid=53ab9b0392879f1ab14bc1826b8b0bf3amp;units=metric'.format(city)
res= requests.get(url)
data= res.json()
Temp= data['main']['temp']
Weatherdata= data['weather'][0]['main']
await ctx.send(f"Temperature is {Temp} and weather is {Weatherdata}")
@client.command()
async def sr(ctx, number):
x=int(number)
squareroot= float(math.sqrt(x))
await ctx.send(f"The square root of {number} is {squareroot}")
@client.command()
async def dict(ctx, word):
mean= dic.meaning(word)
await ctx.send(mean)
@client.command()
async def t(ctx, lang,*, word,):
wrd= trans.translate(word, dest=lang)
await ctx.send(f"in {lang}, {word} is '{wrd.text}' ")
@client.event
async def on_message(message):
bad_words=["Fuck","fucking","fucker","shit","akshith","bitch","Anika","anika","sabertooth"]
for bad_word in bad_words:
if message.content.count(bad_word)> 0:
await message.channel.purge(limit=1)
client.run(token)
Раньше он работал почти идеально, а теперь ctx.send не работает, но, тем не менее, client.event() для фильтра плохих слов я создал этого бота, изучив учебное пособие Лукаса по discord bot
Ответ №1:
Вам нужно использовать process_commands()
в конце on_message
событий, чтобы заставить другие команды работать. Подробнее
@client.event
async def on_message(message):
bad_words=["Fuck","fucking","fucker","shit","akshith","bitch","Anika","anika","sabertooth"]
for bad_word in bad_words:
if message.content.count(bad_word)> 0:
await message.channel.purge(limit=1)
await client.process_commands(message)