Discord bot突然不再发送消息了

时间:2020-08-26 11:43:18

标签: python discord discord.py discord.py-rewrite

上下文:我最近做过,也许经过一些运行后,它突然不发送消息也不返回语法错误,这是代码

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={}&appid=53ab9b0392879f1ab14bc1826b8b0bf3&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无法正常工作,但是我从Lucas的discord bot教程中学到了这个badwords过滤器的client.event()后,就制成了该机器人。

1 个答案:

答案 0 :(得分:0)

您需要在process_commands()事件的末尾使用on_message才能使其他命令起作用。 Read more

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