简体   繁体   English

我正在尝试在 repli 中创建一个基本的 discord 机器人,并且我已经为它编写了一个代码,但是当我运行它时它一直向我显示错误

[英]I am trying to create a basic discord bot in replit and I have written a code for it but it keeps showing me an error when I run it

I am trying to create a Discord Bot in replit我正在尝试在replit中创建一个 Discord Bot

import discord 
from discord.ext import commands 
  client = commands.Bot(command_prefix = "+")
  @client.event
  async def on_ready():
    print('Bot is ready')
  @client.event    
  async def on_member_join(member):
    print(f'{member}has joined the server')
  @client.event  
  async def on_member_remove(member):
    print(f'{member}has left the server')  


client.run('token')

This is the code that is giving me an error.这是给我一个错误的代码。

Let's start from the beginning.让我们从头开始。 Let's see how do we do it.让我们看看我们是如何做到的。 :D :D

I will do it step by step and make you understand every single statement.我会一步一步地做,让你理解每一个陈述。 :) :)

Step 1: Importing Package第一步:导入 Package

Import the package discord and import the commands subpackage as well.导入 package discord并同时导入commands子包。

import discord
from discord.ext import commands
Step 2: Define your Bot variable 第 2 步:定义您的Bot变量

We are now going to define the bot so as to use it using our package.我们现在要定义机器人,以便使用我们的 package 来使用它。

 client = commands.Bot(prefix="+", case_insensitive=True)

Note: I have added case_insensitive as it allows us to use the command any way we want.注意:我添加了case_insensitive ,因为它允许我们以任何我们想要的方式使用命令。 For example, you can enter the command +help in ways: +HELP , +Help and all the other possible combinations.例如,您可以通过以下方式输入命令+help+HELP+Help以及所有其他可能的组合。

Step 3: Creating on_ready event for our Bot.第 3 步:为我们的 Bot 创建on_ready事件。

We will now create an on_ready function for the bot so that we confirm the bot is online.我们现在将为机器人创建一个on_ready function,以便我们确认机器人在线。

 @client.event async def on_ready(): print("I am now online, my creator. :D")

Step 4: Create on_member_join event for our Bot.第 4 步:为我们的 Bot 创建on_member_join事件。

We will now create the function to check for member joining the server and do the actions we want it to do.我们现在将创建 function 来检查加入服务器的成员并执行我们希望它执行的操作。

 @client.event async def on_member_join(member): join_member_channel = client.get_channel(id) await join_member_channel.send(f"{member.mention} has joined the server.") await client.process_commands(message)

Note: The id in the join_member_channel is the ID of the channel you want the bot to send message in. So which ever channel you are using to create join log, get the ID and paste it instead of id .注意: join_member_channel中的id是您希望机器人发送消息的频道的 ID。因此,无论您使用哪个频道创建加入日志,获取 ID 并将其粘贴而不是id

Step 5: Create on_member_remove event for our Bot.第 5 步:为我们的 Bot 创建on_member_remove事件。

We will now create a function to check for member leaving the server and do the actions we want it to do.我们现在将创建一个 function 来检查离开服务器的成员并执行我们希望它执行的操作。

 @client.event async def on_member_remove(member): leave_member_channel = client.get_channel(id) await leave_member_channel.send(f"{member.mention} has left the server") await client.process_commands(message)

Note: The id in the leave_member_channel is the ID of the channel you want the bot to send message in. So which ever channel you are using to create join log, get the ID and paste it instead of id .注意: leave_member_channel中的id是您希望机器人发送消息的频道的 ID。因此,无论您使用哪个频道来创建加入日志,获取 ID 并粘贴它而不是id

Step 6: Run the bot.第 6 步:运行机器人。 Simplest YEAH!最简单的耶!

 client.run("ADD YOUR TOKEN HERE")

Compiled Code:编译代码:

 import discord from discord.ext import commands client = commands.Bot(prefix="+", case_insensitive=True) @client.event async def on_ready(): print("I am now online, my creator. :D") @client.event async def on_member_join(member): join_member_channel = client.get_channel(id) await join_member_channel.send(f"{member.mention} has joined the server.") await client.process_commands(message) @client:event async def on_member_remove(member). leave_member_channel = client.get_channel(id) await leave_member_channel.send(f"{member.mention} has left the server") await client.process_commands(message) client.run("ADD YOUR TOKEN HERE")

Hope I was able to explain it well to you.希望我能很好地向你解释。 Please ask if any doubt, in the comments.请在评论中询问是否有任何疑问。 :) :)

Thank You: :D谢谢::D

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的不和谐机器人代码有问题,它一直显示相同的错误 - I have a problem with my code for a discord bot, it keeps showing the same error 为什么我在尝试运行我的 Discord 机器人时会收到此错误 - Why am I getting this error when I am trying to run my Discord bot 为什么我在尝试运行 discord 机器人时会出错? - Why do I get error when I am trying to run my discord bot? 我使用 Replit 在 discord.py 上创建一个简单的音乐机器人,但它一直给我一个错误 - Im using Replit to create a simple music bot on discord.py yet it keeps giving me an error 我在 Repl.it 上创建了一个 discord 音乐机器人,这个错误一直显示 - I was creating a discord music bot on Repl.it and this error keeps showing 当我运行我的测试 discord 机器人时,我不断收到错误消息 - I keep getting an error in replit when im running my test discord bot 我正在尝试登录 discord 但我收到错误代码 - I am trying to log in discord but i am getting an error code 错误 429,请求过多。 当我试图让我的 python discord 24/7 重复时 - Error 429, Too many requests. When I was trying to keep my python discord 24/7 on replit 当我遇到意图错误时,我正试图制作一个 discord 机器人 - I was trying to make a discord bot when I got an intents error 我正在研究 discord 机器人,我需要一个货币系统。不幸的是,每次我运行代码时,它都会给我一个关键错误 - i was working on a discord bot and i needed a money system .unfortunately every time i run the code it gave me the key error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM