reqrite in discord.py v2
This commit is contained in:
1
bot/__init__.py
Normal file
1
bot/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from .client import client
|
||||||
17
bot/client.py
Normal file
17
bot/client.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import discord
|
||||||
|
from discord import app_commands
|
||||||
|
|
||||||
|
guild_id = '283156308465811456'
|
||||||
|
|
||||||
|
class client(discord.Client):
|
||||||
|
def __init__(self, intents=discord.Intents.default()):
|
||||||
|
super().__init__(intents=intents)
|
||||||
|
self.synced = False #we use this so the bot doesn't sync commands more than once
|
||||||
|
self.tree = tree = app_commands.CommandTree(self)
|
||||||
|
|
||||||
|
async def on_ready(self):
|
||||||
|
await self.wait_until_ready()
|
||||||
|
if not self.synced: #check if slash commands have been synced
|
||||||
|
await self.tree.sync(guild = discord.Object(id=guild_id)) #guild specific: leave blank if global (global registration can take 1-24 hours)
|
||||||
|
self.synced = True
|
||||||
|
print(f"We have logged in as {self.user}.")
|
||||||
85
main.py
85
main.py
@@ -1,33 +1,33 @@
|
|||||||
####################
|
"""
|
||||||
# == Main Info ==
|
version 2.0.0
|
||||||
# main.py
|
author 4nima, oce
|
||||||
# by 4nima
|
description template for discord bot
|
||||||
# v.0.0.0
|
requirements
|
||||||
#
|
- discord
|
||||||
####################
|
- flask (for keep_alive)
|
||||||
# == Descrtiption ==
|
"""
|
||||||
# template for discord bot
|
|
||||||
#
|
|
||||||
####################
|
|
||||||
# == Dependencies ==
|
|
||||||
# pip3 install discord
|
|
||||||
# pip3 install flask (for keep_alive)
|
|
||||||
####################
|
|
||||||
|
|
||||||
## Imports
|
## Imports
|
||||||
import asyncio as aio
|
|
||||||
import logging as log
|
|
||||||
import json
|
import json
|
||||||
import random
|
import logging as log
|
||||||
|
|
||||||
from discord.ext import commands
|
import discord
|
||||||
|
from discord import app_commands
|
||||||
#from lib.keep_alive import keep_alive
|
#from lib.keep_alive import keep_alive
|
||||||
|
|
||||||
|
from bot import client
|
||||||
|
|
||||||
|
intents = discord.Intents.default()
|
||||||
|
intents.message_content = True
|
||||||
|
|
||||||
|
guild_id = '283156308465811456'
|
||||||
|
|
||||||
|
bot = client(intents)
|
||||||
|
|
||||||
## Vars
|
## Vars
|
||||||
### Load and set Bot Config
|
### Load and set Bot Config
|
||||||
with open("bot.conf", 'r') as f:
|
with open("bot.conf", 'r') as f:
|
||||||
BOTDATA = json.load(f)
|
BOTDATA = json.load(f)
|
||||||
bot = commands.Bot(command_prefix=BOTDATA['prefix'])
|
|
||||||
|
|
||||||
### Set logging options
|
### Set logging options
|
||||||
log.basicConfig(
|
log.basicConfig(
|
||||||
@@ -37,43 +37,10 @@ log.basicConfig(
|
|||||||
datefmt='%Y-%m-%d %H:%M:%S'
|
datefmt='%Y-%m-%d %H:%M:%S'
|
||||||
)
|
)
|
||||||
|
|
||||||
## Run
|
# run
|
||||||
### Events
|
# bot defination
|
||||||
@bot.event
|
@bot.tree.command(guild = discord.Object(id=guild_id), name = 'tester', description='testing') #guild specific slash command
|
||||||
async def on_ready():
|
async def slash2(interaction: discord.Interaction):
|
||||||
log.info(f'Logged in as {bot.user}')
|
await interaction.response.send_message(f"I am working! I was made with Discord.py!", ephemeral = True)
|
||||||
print(f'Logged in as {bot.user}')
|
|
||||||
|
|
||||||
for guild in bot.guilds:
|
bot.run(BOTDATA['token'])
|
||||||
log.info(f'Connect to Server {guild.name} (id: {guild.id})')
|
|
||||||
|
|
||||||
@bot.event
|
|
||||||
async def on_reaction_add(reac, usr):
|
|
||||||
print(reac)
|
|
||||||
print(usr)
|
|
||||||
|
|
||||||
### Commands
|
|
||||||
@bot.command(name='hello', help='Say hello!')
|
|
||||||
async def say_hello(ctx):
|
|
||||||
async with ctx.typing():
|
|
||||||
log.info('Start say_hello function')
|
|
||||||
await ctx.message.add_reaction('🤔')
|
|
||||||
reply = await ctx.send(f'Hello, <@{ctx.author.id}>!')
|
|
||||||
print(ctx.guild)
|
|
||||||
print(ctx.message.content)
|
|
||||||
print(ctx.message)
|
|
||||||
print(ctx.author)
|
|
||||||
print(reply)
|
|
||||||
await aio.sleep(random.randint(1,5))
|
|
||||||
await ctx.message.remove_reaction('🤔', bot.user)
|
|
||||||
if random.randint(1,100) < 10:
|
|
||||||
log.error('say_hello function was failed')
|
|
||||||
await ctx.message.add_reaction('❌')
|
|
||||||
else:
|
|
||||||
log.info('Run say_hello successfully')
|
|
||||||
await ctx.message.add_reaction('✅')
|
|
||||||
await aio.sleep(10)
|
|
||||||
await ctx.message.delete()
|
|
||||||
|
|
||||||
#keep_alive()
|
|
||||||
bot.run(BOTDATA['token'])
|
|
||||||
Reference in New Issue
Block a user