DC Bot py

嘿嘿嘿

下載模組

pip install discord.py

reset token 之後複製

這些勾起來

這些勾起來

開始寫八

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('輸出')

刪除訊息

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

ctx

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

(id, 頻道之類的東西)

實作

下禮拜教slash command

Made with Slides.com