Discord 机器人不会听任何命令或不会输出任何事件

问题描述 投票:0回答:1

机器人运行时没有任何错误,但它只是不会听你给它的任何命令,例如:“!加入”,“!离开”,“!播放”等。甚至没有事件,什么也没有。昨天有效,但今天...我不知道...

这是代码:


import discord
from discord.ext import commands
from discord import FFmpegPCMAudio
from better_profanity import profanity
import random
import os

intents = discord.Intents.all()
intents.members = True

bot = commands.Bot(command_prefix='!', intents=intents)

import apikeys as ak
import rapidAPI_Joker as rj

@bot.event
async def on_ready():
    print("--------------------------------")
    print("The bot is now ready for use!")
    print("--------------------------------")

@bot.event
async def on_member_join(member):
    channel = bot.get_channel(1161221526150987807)
    await channel.send(f"Hello {member.display_name}! Welcome to my discord server!")
    await channel.send(rj.setup)
    await channel.send(rj.punchline)

@bot.event
async def on_member_remove(member):
    channel = bot.get_channel(1161221526150987807)
    await channel.send(f"Goodbye {member.display_name}!")

@bot.event
async def on_message(message):
    greeting = ['Are you daft?', 'With that mouth you kiss your mother?', 'Imagine you told that to your girlfriend!']
    if profanity.contains_profanity(message.content):
        await message.delete()
        await message.channel.send(random.choice(greeting))

@bot.command(pass_context=True)
async def join(ctx):
    if ctx.author.voice:
        channel = ctx.message.author.voice.channel
        voice = await channel.connect()
        source = FFmpegPCMAudio('BO2REE.mp3')
        player = voice.play(source, after=lambda x=None: check_queue(ctx, ctx.message.guild.id))
        print('Join')
    else:
        await ctx.send('You are not in a voice channel!')

@bot.command(pass_context=True)
async def leave(ctx):
    if ctx.voice_client:
        await ctx.guild.voice_client.disconnect()
        await ctx.send('I left the voice channel!')
    else:
        await ctx.send('I\'m not in a voice channel!')

@bot.command(pass_context=True)
async def pause(ctx):
    voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
    if voice.is_playing():
        voice.pause()
    else:
        await ctx.send('There\'s no audio playing!')

@bot.command(pass_context=True)
async def resume(ctx):
    voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
    if voice.is_paused():
        voice.resume()
    else:
        await ctx.send('No audio paused!')

@bot.command(pass_context=True)
async def stop(ctx):
    voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
    voice.stop()

queues = {}

def check_queue(ctx, id):
    if queues.get(id):
        if queues[id] != []:
            voice = ctx.guild.voice_client
            source = queues[id].pop(0)
            player = voice.play(source)

@bot.command(pass_context=True)
async def play(ctx, audio):
    voice = ctx.guild.voice_client
    audio_file = audio + '.mp3'
    if not os.path.isfile(audio_file):
        await ctx.send('The specified audio file does not exist.')
        return
    source = FFmpegPCMAudio(audio_file)
    player = voice.play(source, after=lambda x=None: check_queue(ctx, ctx.message.guild.id))
    await ctx.send('Now playing: ' + audio)

@bot.command(pass_context=True)
async def queue(ctx, audio):
    voice = ctx.guild.voice_client
    audio_file = audio + '.mp3'
    if not os.path.isfile(audio_file):
        await ctx.send('The specified audio file does not exist.')
        return
    source = FFmpegPCMAudio(audio_file)
    guild_id = ctx.message.guild.id
    if guild_id in queues:
        queues[guild_id].append(source)
    else:
        queues[guild_id] = [source]
    await ctx.send(f'Added {audio_file} to queue')

bot.run(ak.bot_token)
  1. 从 Discord 开发者门户重置令牌并将其粘贴回代码中。

  2. 打开/关闭所有特权网关意图,看看我是否可以让它工作。

  3. 在 Discord 开发者门户中创建了另一个机器人,并进行了与第一个机器人相同的设置。

  4. 重新安装discord.py包

  5. 重新安装每个软件包(逐个软件包)

  6. 我昨天运行了代码,它运行正常,我创建的模块(文件)没有任何问题!

    注意:我创建的模块是指诸如“apikeys.py”和“rapidAPI_Joker.py”之类的文件。

python discord discord.py bots
1个回答
0
投票

我会回答我自己的问题,因为人们有自己的问题需要解决。

添加了这行代码

"await bot.process_commands(message)"

之前

@bot.event
async def on_message(message : discord.Message):
    greeting = ['Are you daft?' , 'With that mouth you kiss your mother?' , 'Imagine you told that to your girlfriend!']
    if profanity.contains_profanity(message.content):
        await message.delete()
        await message.channel.send(r.choice(greeting))

之后

@bot.event
async def on_message(message : discord.Message):
    greeting = ['Are you daft?' , 'With that mouth you kiss your mother?' , 'Imagine you told that to your girlfriend!']
    if profanity.contains_profanity(message.content):
        await message.delete()
        await message.channel.send(r.choice(greeting))
    await bot.process_commands(message)

现在,机器人会监听您发出的任何命令甚至事件。

原因:添加到程序中的代码行允许 Discord 机器人继续处理和处理其他命令,即使消息因包含脏话而被删除。它保持机器人的整体功能完整并响应用户交互。

© www.soinside.com 2019 - 2024. All rights reserved.