用 python 编写的 Discord bot 执行事件,但不执行命令?

时间:2021-01-07 18:19:31

标签: python discord bots

大约 2 个月前,我创建了一个包含一些命令和事件的 Discord 机器人。当时一切正常,几周后修改后,一切仍然有效。然而,现在运行程序时唯一起作用的是事件。有人知道是什么问题吗?|

这是我的代码:

import os
import discord
import random
from discord.ext import commands
from bs4 import BeautifulSoup
import requests
import urllib.parse, urllib.request, re
from discord.utils import get

GUILD = '775884169422307378'
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='&', intents = intents)
blacklist = ['vanilla', 'v a n i l l a', 'v*nilla', 'va*illa', 'nilla', 'van*lla', 'vani*la', 'vanil*a', 'vanill*', 'van']

@client.command(name='views', help = 'Sends a link of video requested along with its view count.')
async def views(ctx, *, search):
    query_string = urllib.parse.urlencode({'search_query': search})
    htm_content = urllib.request.urlopen(
        'http://www.youtube.com/results?' + query_string)
    search_results = re.findall(r'/watch\?v=(.{11})',
                                htm_content.read().decode())
    url = 'http://www.youtube.com/watch?v=' + search_results[0]
    choice = BeautifulSoup(requests.get(url).text, 'lxml')
    output = int(choice.select_one('meta[itemprop="interactionCount"][content]')['content'])
    output = '{:,}'.format(output)
    await ctx.send('http://www.youtube.com/watch?v=' + search_results[0])
    await ctx.send('This video has ' + str(output) + ' views.')

@client.command(name='peter', help = 'Peter coomer')
async def peter(ctx):
    await ctx.send('http://www.youtube.com/watch?v=TQXTXkWZ67E')

@client.command(name='version', help = 'Displays information about this bot.')
async def version(ctx):
    myEmbed = discord.Embed(title = "Current version", description = "The bot is in version 1.0", color = 0x00ff00)
    myEmbed.add_field(name = "Version Code: ", value = "v1.0.0", inline = False)
    myEmbed.add_field(name = "Date Released: ", value = "November 1st, 2020", inline = False)
    myEmbed.set_author(name = "Nick Smith")
    
    await ctx.message.channel.send(embed = myEmbed)    

@client.command(name = 'roll', help = 'Simulates rolling a dice. (!roll [number of dice] [number of sides])')
async def roll(ctx, numberOfDice: int, numberOfSides: int):
    dice = [
        str(random.choice(range(1, numberOfSides + 1)))
        for i in range(numberOfDice)
    ]
    await ctx.message.channel.send(', '.join(dice))

@client.command(name = 'rps', help = 'Plays a game of rock paper scissors.')
async def rps(ctx):
    await ctx.send(
        'Hello, ' + ctx.message.author.mention + '! Would you like to play rock, paper, scissors?')

    wins = 0
    losses = 0
    ties = 0

    while True:
        await ctx.send('%s Victory, %s Defeat, %s Draw \n' % (wins, losses, ties))
        while True:
            await ctx.send('What do you pick? (r)ock, (p)aper, (s)cissors, or (q)uit')  
            player = await client.wait_for('message', timeout = 30) 
            print(str(player.content))
            if player.content == 'q':
                rpsEmbed = discord.Embed(title = "Score Report", description = "Your rock, paper, scissors record.", color = 0x03ff13)
                rpsEmbed.add_field(name = "Wins: ", value = str(wins))
                rpsEmbed.add_field(name = "Losses: ", value = str(losses))
                rpsEmbed.add_field(name = "Draws: ", value = str(ties))
                await ctx.message.channel.send(embed = rpsEmbed)
                return
            if player.content == 'r' or player.content == 'p' or player.content == 's':
                break
            await ctx.send('Type r, p, s, or q!')

        if player.content == 'r':
            await ctx.send('Rock against...')
        elif player.content == 'p':
            await ctx.send('Paper against...')
        elif player.content == 's':
            await ctx.send('Scissors against...')

        randomnum = random.randint(1, 3)
        if randomnum == 1:
            computer = 'r'
            await ctx.send('Rock!')
        elif randomnum == 2:
            computer = 'p'
            await ctx.send('Paper!')
        elif randomnum == 3:
            computer = 's'
            await ctx.send('Scissors!')

        if player.content == computer:
            await ctx.send('Draw!')
            ties = ties + 1
        elif player.content == 'r' and computer == 's':
            await ctx.send('You win!')
            wins = wins + 1
        elif player.content == 'r' and computer == 'p':
            await ctx.send('You lose!')
            losses = losses + 1
        elif player.content == 'p' and computer == 'r':
            await ctx.send('You win!')
            wins = wins + 1
        elif player.content == 'p' and computer == 's':
            losses = losses + 1
            await ctx.send('You lose!')
        elif player.content == 's' and computer == 'p':
            await ctx.send('You win!')
            wins = wins + 1
        elif player.content == 's' and computer == 'r':
            await ctx.send('You lose!')
            losses = losses + 1

@client.command(name = 'clear', help = 'Clears x amount of messages.')
async def clear(ctx, amount = 5):
    await ctx.channel.purge(limit = amount + 1)

@client.event
async def on_ready():
    general_channel = client.get_channel(775884169422307381)
    await general_channel.send('Hello, world!')
    
    for guild in client.guilds:
        if guild.name == GUILD:
            break

    print(
        f'{client.user} is connected to the following guild:\n'
        f'{guild.name}(id: {guild.id})\n'
    )

    members = '\n - '.join([member.name for member in guild.members])
    print(f'Guild Members:\n - {members}')

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if 'why are you here?' in message.content.lower():
        response = ("I'm a bot for Nick's Directed Studies class")
        await message.channel.send(response)
    await client.process_commands(message)

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    for i in blacklist:
        if i in (message.content.lower()).lower():
            await message.channel.purge(limit = 1)
            break

client.run('Nzc1ODgzNjc1MTY1MjYxODI2.X6sz-w.d_KtsDGermW1sS-9cVE5-4vZfw0')

0 个答案:

没有答案