# 老子梁起秋 commands log --- ## 2023/06/01 ``` @bot.command() async def McD(ctx): await ctx.send("我要吃麥當勞:fries") ``` ``` @commands.command() async def val(self,ctx): #await ctx.channel.purge(limit = 1) await ctx.send(f" <@{789419962934493194}>問要不要打瓦") await ctx.send(file=discord.File("C:\\Users\\Jeffrey\\Code\\Python\\discord_bot\\invite.jpg")) ``` ``` @commands.Cog.listener() async def on_message(self,msg): if msg.content=='阿夸': path=r"C:\Users\Jeffrey\Code\Python\discord_bot\aqua\aqua_smoke_1.jpg" await msg.channel.send("愛抽菸") await msg.channel.send(file=discord.File(path)) elif msg.content=='Ok': await msg.channel.send("OK") ``` ## 2023/06/02 ``` @commands.Cog.listener() async def on_message(self,msg): if msg.content=='阿夸': path=r"C:\Users\Jeffrey\Code\Python\discord_bot\aqua\aqua_smoke_1.jpg" await msg.channel.send("愛抽菸") await msg.channel.send(file=discord.File(path)) elif msg.content=='Ok': await msg.channel.send("OK") ``` ``` @commands.command() async def McD(self,ctx): localtime = time.localtime() result = time.strftime("%I:%M:%S", localtime) commons_file = open('discord_bot\item.json', "r", encoding = "utf-8") data_commons = json.load(commons_file) commons_file.close() data_commons['McD'] +=1 await ctx.send(f"現在是{result}, 這是我吃的第{data_commons['McD']}份麥當勞:fries:") commons_file = open('discord_bot\item.json', "w", encoding = "utf-8") json.dump(data_commons,commons_file,indent=1) ``` ## 2023/06/04 ``` @commands.command() async def dish(self,ctx): message_id = ctx.message.id channel = ctx.message.channel # 取得訊息所在的頻道 message = await channel.fetch_message(message_id) await ctx.reply(f" <@{ctx.author.id}>去洗碗",mention_author=False) ``` ## 2023/06/06 ``` @commands.Cog.listener() async def on_message(self,msg): if msg.content=='阿夸': path_1=r"C:\Users\Jeffrey\Code\Python\discord_bot\aqua" aqua_pic=os.listdir(path_1) path_1=path_1+"\\"+(str)(aqua_pic[random.randint(0,len(aqua_pic))]) await msg.channel.send("愛抽菸") await msg.channel.send(file=discord.File(path_1)) elif msg.content=='Ok': await msg.channel.send("OK") ``` ## 2023/06/15 commons .py ``` import discord from discord.ext import commands from discord import app_commands import time import os import random import json class commons(commands.Cog): def __init__(self,bot): self.bot=bot @commands.Cog.listener() async def on_ready(self): print("Commons is ready!") @app_commands.command(name="val",description="I want to play valorant!") async def val(self,interaction:discord.Interaction): pic="C:\\Users\\Jeffrey\\Code\\Python\\discord_bot\\invite.jpg" await interaction.response.send_message(f" <@{interaction.user.id}>問要不要打瓦",file=discord.File(pic)) @app_commands.command(name="mcd", description="I love Mcdonald!") async def mcd(self,interaction:discord.Interaction): localtime = time.localtime() result = time.strftime("%I:%M:%S", localtime) commons_file = open('discord_bot\item.json', "r", encoding = "utf-8") data_commons = json.load(commons_file) commons_file.close() data_commons['McD'] +=1 await interaction.response.send_message(content=f"現在是{result}, 這是我吃的第{data_commons['McD']}份麥當勞:fries:") commons_file = open('discord_bot\item.json', "w", encoding = "utf-8") json.dump(data_commons,commons_file,indent=1) @app_commands.command(name='dish',description="You can let yourself wash dish.") async def dish(self,interaction:discord.Interaction): await interaction.response.send_message(f" <@{interaction.user.id}>去洗碗") @app_commands.command(name='aqua',description="I love aqua!") async def aqua(self,interaction:discord.Interaction): path_1=r"C:\Users\Jeffrey\Code\Python\discord_bot\aqua" aqua_pic=os.listdir(path_1) path_1=path_1+"\\"+(str)(aqua_pic[random.randint(0,len(aqua_pic))]) await interaction.response.send_message("愛抽菸",file=discord.File(path_1)) @commands.Cog.listener() async def on_message(self,msg): if msg.content=='Ok': await msg.channel.send("OK") async def setup(bot): await bot.add_cog(commons(bot)) ``` bot .py ``` import json import os import discord from discord.ext import commands from discord.ext.commands import bot import asyncio import time intents = discord.Intents.default() intents.messages=True intents.message_content=True bot = commands.Bot(command_prefix='!',intents=intents) @bot.command() async def load(ctx,extension): await bot.load_extension(f'cogs.{extension}') await ctx.send(f'Load {extension} is done!') @bot.command() async def unload(ctx,extension): await bot.unload_extension(f'cogs.{extension}') await ctx.send(f'Unload {extension} is done!') @bot.command() async def reload(ctx,extension): await bot.unload_extension(f'cogs.{extension}') await bot.load_extension(f'cogs.{extension}') await bot.tree.sync() await ctx.send(f'Reload {extension} is done!') async def preload(): for filename in os.listdir(r'C:\Users\Jeffrey\Code\Python\discord_bot\cogs'): if filename.endswith('.py'): await bot.load_extension(f'cogs.{filename[:-3]}') @bot.event async def on_ready(): await bot.tree.sync() async def main(): async with bot: await preload() with open('discord_bot\item.json', "r", encoding = "utf-8") as file: data = json.load(file) await bot.start(data['token']) asyncio.run(main()) ``` ## 2023/07/03 food .py ``` import discord from discord.ext import commands from discord import app_commands import time import os import random import json class food(commands.Cog): def __init__(self,bot): self.bot=bot @commands.Cog.listener() async def on_ready(self): print("food is ready!") @app_commands.command(name="eat",description="Choose what you wanna eat.") @app_commands.choices( food_eat=[ discord.app_commands.Choice(name='McD',value="McD"), discord.app_commands.Choice(name='Mos',value="Mos"), discord.app_commands.Choice(name='Macc',value="Macc") ] ) async def eat(self,interaction:discord.Interaction,food_eat:discord.app_commands.Choice[str]): localtime = time.localtime() result = time.strftime("%I:%M:%S", localtime) food_file = open('discord_bot\\food_time.json', "r", encoding = "utf-8") data_food = json.load(food_file) food_file.close() data_food[food_eat.value] +=1 await interaction.response.send_message(content=f"現在是{result}, 這是我吃的第{data_food[food_eat.value]}份{food_eat.value}:fork_knife_plate:") food_file = open('discord_bot\\food_time.json', "w", encoding = "utf-8") json.dump(data_food,food_file,indent=1) food_file.close() @app_commands.command(name="vomit",description="Choose what you wanna vomit.") @app_commands.choices( food_vomit=[ discord.app_commands.Choice(name='McD',value="McD"), discord.app_commands.Choice(name='Mos',value="Mos"), discord.app_commands.Choice(name='Macc',value="Macc") ] ) async def vomit(self,interaction:discord.Interaction,food_vomit:discord.app_commands.Choice[str]): localtime = time.localtime() result = time.strftime("%I:%M:%S", localtime) food_file = open('discord_bot\\food_time.json', "r", encoding = "utf-8") data_food = json.load(food_file) food_file.close() if(data_food[food_vomit.value]!=0): data_food[food_vomit.value] -=1 await interaction.response.send_message(content=f"現在是{result}, 我把{food_vomit.value}吐出來了:face_vomiting:\n") food_file = open('discord_bot\\food_time.json', "w", encoding = "utf-8") json.dump(data_food,food_file,indent=1) food_file.close() async def setup(bot): await bot.add_cog(food(bot)) ```