[英]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.我会一步一步地做,让你理解每一个陈述。 :) :)
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
以及所有其他可能的组合。
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")
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
。
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")
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.