0

First of all, i want to know if/how i can bind this bot event to a specific channel or a specific role so this can just be executed by this role in this channel:


async def on_message(message):

if content.message.startswith('!Beispiel')

(return)

Also i want to know why my bot doesnt answer with this code:

import asyncio
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix = "!")

client = discord.Client()


@bot.command()
async def temprole(ctx, role_time : int, member: discord.Member=None, role: discord.Role = None):
    if not member:
        await ctx.send("Who do you want me to give a role?")
        return
    role = discord.utils.get(ctx.guild.roles, name="muted")
    await member.add_roles(role)
    await ctx.send("Ok, I did it :thumpsup:")
    if role is None:
       return await self.bot.say('Pls write a role')

    await asyncio.sleep(role_time)
    await member.remove_roles(role)
    await ctx.send("The time of the role of {} is up".format(member))

@bot.command()
async def test(ctx, arg):
    await ctx.send(arg)


client.run(' ')

becuase im typing in !test hello or !temprole or so and it doesnt answer :/

  • There is no connection between your `bot` and `client`. Delete your `client` and do `bot.run(TOKEN)`. You can bind the bot to a specific channel or role by adding a custom check to the command. [See example of this section](https://discordpy.readthedocs.io/en/latest/ext/commands/api.html?highlight=check#discord.ext.commands.check) – Tin Nguyen Aug 31 '20 at 14:05
  • Also to get an idea of `discord.Client` and `discord.ext.commands.Bot`: https://stackoverflow.com/questions/51234778/what-are-the-differences-between-bot-and-client It can be confusing when the tutorials switch between the two interchangably. `Bot` is a subclass/extension of `Client`. You should just use Bot throughout your code. – Tin Nguyen Aug 31 '20 at 14:09
  • Sry, but my first question wasnt asnwered and also i have now the question if i can run a bot and a client at the same time with the same token? – Thisisjust Aug 31 '20 at 14:22
  • It makes no sense to run bot and client at the same time. It would also not be possible. Bot is an extension/subclass of Client. Just use bot. I linked an example of how to write a custom check that makes a command only possible under custom criteria. Try to implement that and if you run into problems, make a new question with code + error traceback. StackOverflow isn't a code writing service. It's nice if people do it but you can't expect people to provide you the entire solution. – Tin Nguyen Aug 31 '20 at 14:28

1 Answers1

0

You do not need a client all you want is a bot (commands) here is how it should look.

Also you should consider learning fstring because it is much simpler to format strings

import discord
from discord.ext import commands
import asyncio

bot = commands.Bot(command_prefix = "!")

@bot.command()
async def temprole(ctx, role_time: int, member: discord.Member = None, role: discord.Role = None):
    if not member:
        await ctx.send("Who do you want me to give a role?")
        return
    if role is None:
        await ctx.send('Pls write a role')
        return

    await member.add_roles(role)
    await ctx.send(f"Ok, I did it :thumpsup: {member.mention} is muted for {role_time}sec")

    await asyncio.sleep(role_time)
    await member.remove_roles(role)
    await ctx.send(f"The time of the role of {member.mention} is up")

@bot.command()
async def test(ctx, arg):
    await ctx.send(arg)

@bot.event
async def on_message(message):
    if message.author == bot.user: # to skip messages from the bot
        return

    if message.content.startswith('!Beispiel'):
        await message.channel.send("Beispiel it is  ") 

    await bot.process_commands(message)# to make the bot accept commands

bot.run('TOKEN')
Abdulaziz
  • 3,363
  • 1
  • 7
  • 24