dc bot 補丁(?

基本架構

import discord
from discord.ext import commands

intents = discord.Intents.all()

intents.members = True

bot = commands.Bot(command_prefix="你想要的符號",intents = intents)


bot.run("TOKEN")

事件觸發

@bot.event
async def on_ready(): #開機會觸發
    print(f'The bot has been log in as {bot.user}')
    game = discord.Game("狀態")
    await bot.change_presence(status=discord.Status.online, activity=game)
    
@bot.event 
async def on_member_join(member):
    channel = bot.get_channel(your channel id)
    await channel.send(f'{member}join!') 
    
@bot.event 
async def on_member_remove(member):
    channel = bot.get_channel(your chnne id)
    await channel.send(f'{member}leave!') 

指令觸發

@bot.command()
async def ping(ctx):
    await ctx.send(f'{round(bot.latency*1000)}(ms)')

@bot.command()
async def clear(ctx, num:int):
    await ctx.channel.purge(limit = num+1)

關鍵字觸發

@bot.listen()
async def on_message(msg):
    if msg == '判斷的字串':
      await msg.channel.send('輸出')

ctx

指的是上下文 可以知道上文輸入的資訊

(id, 頻道之類的東西)

斜線指令

bot.tree.command(name = '早安', description = '跟你說早安')
async def hello(interaction: discord.Interaction):
    await interaction.response.send_message(f'{interaction.user.mention} 早安!')

基本上長這樣

斜線指令

一個slash command一定只有一個response

如果你還想傳送其他訊息可以用channel.send()

按鈕

class ButtonView(discord.ui.View):
    @discord.ui.button(label="點擊", style=discord.ButtonStyle.primary)
    async def button_callback(self, button, interaction):
        await interation.response.send_message("我備案了")
        button.disabled = True
        await interation.message.edit(view=self)

之前沒講到

想讓按鈕失效的話這樣寫

button

class ButtonView(discord.ui.View):
    def __init__(self, *, timeout=180):
        super().__init__(timeout=timeout)
    @discord.ui.button(label="演奏", style=discord.ButtonStyle.primary)
    async def button_callback(self, interation: discord.Integration, button: discord.ui.Button):
        await interation.response.send_message("咚咚咚bang")
        
@bot.tree.command(name = '按鈕', description = '按按鈕')
async def button(interaction: discord.Integration):
    await interaction.response.send_message(view = ButtonView())

執行結果

class ButtonView(discord.ui.View):
    def __init__(self, *, timeout=180):
        super().__init__(timeout=timeout)
    @discord.ui.button(label="演奏", style=discord.ButtonStyle.primary)
    async def button_callback(self, interation: discord.Integration, button: discord.ui.Button):
        await interation.response.send_message("咚咚咚bang")
        
@bot.tree.command(name = '按鈕', description = '按按鈕')
async def button(interaction: discord.Integration):
    await interaction.response.send_message(view = ButtonView())

下拉選單

class SelectView(discord.ui.View):
  def __init__(self, *, timeout = None):
    super().__init__(timeout = timeout)
  @discord.ui.select(placeholder="選單", min_values=1, max_values=1, options=[
    discord.SelectOption(label='A', description='this is A', emoji=None),
    discord.SelectOption(label='B', description='this is B', emoji=None),
  ])
  async def select_callback(self, interation, select):
    await interation.response.send_message(f'你選了{select.values[0]}')
    
@bot.tree.command(name='選單', description='下拉式選單')
async def button(interaction: discord.Integration):
  await interaction.response.send_message(view=SelectView())

min_values, max_values

最少(多)選幾項

Made with Slides.com