Python Forum
Discord bot - noob stuff, please don't hate
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Discord bot - noob stuff, please don't hate
#1
Sad 
So I finally moved from team-speak to discord and kind of find it funny tool. It remind me from IRC with bots and I got spark to try make one my self.
So I found this project:
https://github.com/Amethyst93/Discord-YouTube-Notifier
'simple' bot that spams youtube stream and new post notification on channel but I felt like I took too big bite and reverted a little.

So i went for simplest possible and I did manage get bot on channel that reply to command !help, !hello and !add. basics.

Then I start slowly add more commands and experiment different things and oh well. Only 'eyes' emote work. Please review kindly and give me pointers I am obviously missing, here's the code:

# judge_dredd.py
import discord
import random
from discord.ext import commands
import discord.utils
print("Starting bot...")

#using token from a file 'token'
TOKEN = open("token","r").readline()

#client = discord.Client() #define discord client
description = '''Street Judge''' #default identify
bot = commands.Bot(command_prefix='!', description=description)

#We delete default help command
#bot.remove_command('help')

#print some login-info
@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

#simple !hello command just to test bot
@bot.command() 
async def hello(ctx):
    """Returns hello"""
    await ctx.send('Hello!!')

#promote to member   
@bot.command()
async def I_shall_obey_the_rules(ctx, member: discord.Member):
    """Promote user to Member"""
    role = discord.utils.get(ctx.guild.roles, name='@Member')
    await member.add_roles(role)
    await ctx.send("You are now member")

#answers with the ms latency
@bot.command()
async def ping(ctx):
    """Returns bot respond time in milliseconds"""
    await ctx.send('Pong! {round (bot.latency * 1000)}ms ')


#Embeded help with list and details of commands
#@bot.command(pass_context=True)
#async def help(ctx):
#    embed = discord.Embed(
#        colour = discord.Colour.green())
#    embed.set_author(name='Help : list of commands available')
#    embed.add_field(name='.ping', value='Returns bot respond time in milliseconds', inline=False)
#    embed.add_field(name='.quote', value='Get inspired by a powerful quote', inline=False)
#    await ctx.send(embed=embed)

 
#Answers with a random quote
@bot.command()
async def quote(ctx):
    """Get inspired by a powerful quote"""
    responses = open('quotes').read().splitlines()
    random.seed(a=None)
    response = random.choice(responses)
    await bot.process_commands(response)

#If there is an error, it will answer with an error
@bot.event
async def on_command_error(ctx, error):
    await bot.process_commands('Error. Try .help ({error})')

#react to any message that contains 'rules'
@bot.event
async def on_message(ctx):
    if 'rules' in ctx.content:
        emoji = '\N{EYES}'
        await ctx.add_reaction(emoji)

#react to any message that contains 'dredd'
#@bot.event
#async def on_message(ctx):
#    if 'dredd' in ctx.content:
#        emoji = '\N{ANGRY}'
#        await ctx.add_reaction(emoji)
        
@bot.event
async def on_message(ctx):
        if 'dredd' in ctx.content:
            await bot.send('I Am The Law!!!')

#react to new member joining in
@bot.event
async def on_member_join(member):
    await bot.create_dm()
    await bot.send(f'Hi {member.name}, welcome to ETS2 Community -Discord server, ahead rules text channel for further instructions'
    )


print("Bot is ready!")
bot.run(TOKEN) #run the client using using my bot's token from token file called 'token'
Reply
#2
Yeah seems pretty decent at least you didn't share your token, keep going
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020