Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

python - Discord.py switching from client to bot

I'm fairly new at making a discord bot. I have created a method to get a list of all the members in the server the bot is connected to. This method works fine. However, I am also trying to get the bot to accept commands. I am having a lot of trouble with this and I keep seeing that I should use commands.Bot for this instead of Client. I can't figure out how to make my original method work with commands.Bot though. Any help would be appreciated!

import os

import discord
import csv
from collections import defaultdict
from discord.ext import commands




intents = discord.Intents.all()
client = discord.Client(intents=intents)
outP = open("giveawayList.txt", "w")
bot = commands.Bot(command_prefix='!')


@client.event
async def on_message(message):
    if message == "!test":
        await message.channel.send("you failed the test")
        
        
@client.event
async def on_ready():
    for guild in client.guilds:
        if guild.name == GUILD:
            break

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


    count = 0
    for guild in client.guilds:
        for member in guild.members:
            print(member)
            if 'Bots' not in str(member.roles):
                
                print(member.name, ' ')
                outP.write(member.name)
                outP.write("
")
                count += 1
    print('Number of members: ' + str(count))
question from:https://stackoverflow.com/questions/65852705/discord-py-switching-from-client-to-bot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is good to use commands.bot instead of Client because it is an extended version, as it inherits all the functionalities from Client.

I see you have tried to migrate from the use of Client to using commands.bot, but have a few things in mind:

You do not want to use both at the same time, so this is wrong:

client = discord.Client(intents=intents)
bot = commands.Bot(command_prefix='!')

You should keep only the bot one.

Apart from that, you gotta replace the client from the decorators and the calls inside the function. Your corrected code should look something like this:

import os

import discord
import csv
from collections import defaultdict
from discord.ext import commands
    
intents = discord.Intents.all()
outP = open("giveawayList.txt", "w")
bot = commands.Bot(command_prefix='!', intents=intents)
    
@bot.event
async def on_message(message):
    if message == "!test":
        await message.channel.send("you failed the test")

    await bot.process_commands(message)
            
@bot.event
async def on_ready():
    for guild in bot.guilds:
        if guild.name == GUILD:
            break
    
    print(
        f'{bot.user} is connected to the following guild: 
' 
        f'{guild.name} (id: {guild.id})'
    )
    
    count = 0
    for guild in bot.guilds:
        for member in guild.members:
            print(member)
            if 'Bots' not in str(member.roles):
                 
                print(member.name, ' ')
                outP.write(member.name)
                outP.write("
")
                count += 1
    print('Number of members: ' + str(count))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...