discord bot 部署流程

https://bot-hosting.net/ 这个是免费的24/7的服务器,专供discord bot
https://wiki.bot-hosting.net/guides/hosting-your-first-bot # 如何部署bot-hosting

https://hackmd.io/@smallshawn95/python_discord_bot_base # 教程
https://www.youtube.com/watch?v=26Sj5hJFqUs # 如何写slash命令

  1. 先在discord developers注册app https://discord.com/developers/applications

  2. 复制 settings->Bot->Token里的值

  3. 以下的脚本放在电脑上执行(记得先pip install discord)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import discord
from discord.ext import commands

BOT_TOKEN = # 填写刚刚复制的
GUILD_ID = # 填写服务器的server id,discord 个人设置 开发者模式打开,再右键伺服器的图标,最下面就能复制了

class Client(commands.Bot):
async def on_ready(self):
print(f"已{self.user}的身份登录!")
try:
guild = discord.Object(id = GUILD_ID)

synced= await self.tree.sync(guild=guild)
print(f"同步了{len(synced)}条命令在{guild.id}")
except Exception as e:
print(f"error syncing commands: {e}")
async def on_message(self, message):
if message.content.startswith("Lusalia"):
await message.channel.send(f'Hi there {message.author}, what can I help you?')

async def on_reaction_add(self, reaction, user):
await reaction.message.channel.send("you reacted")

intents = discord.Intents.default()
intents.message_content = True
client = Client(command_prefix="!", intents=intents)

guild = discord.Object(id = GUILD_ID)
@client.tree.command(name="hello", description="问好", guild=guild)
async def sayHello(interaction: discord.Interaction):
await interaction.response.send_message("Hello world!")

@client.tree.command(name="say", description="say something", guild=guild)
async def say(interaction: discord.Interaction, text: str):
await interaction.response.send_message(f"You just said {text}!")

client.run(BOT_TOKEN)

然后将bot添加到伺服器里,就算在本地部署成功了