I want Discordbot to work only on certain channels.

Asked 2 years ago, Updated 2 years ago, 361 views

I put the bot I created into the server, but it works in response to all channels, so I want to be able to operate only on certain channels!

import discord
from discord.ext import commands
import asyncio
importos
import subprocess
import ffmpeg
from voice_generator import create_WAV

client=commands.Bot(command_prefix='.')
voice_client = None



@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('-------')


@client.command()
async def join (ctx):
    print('Get #voicechannel')
    vc=ctx.author.voice.channel
    print('Connect to #voicechannel')
    wait vc.connect()

@client.command()
async def bye (ctx):
    print('#Disconnect')
    wait ctx.voice_client.disconnect()

@client.event
async def on_message(message):
    msgclient=message.build.voice_client
    if message.content.startswith('.'):
        pass

    else:
        if message.build.voice_client:
            print(message.content)
            create_WAV(message.content)
            source=discord.FFmpegPCMAudio("output.wav")
            message.build.voice_client.play(source)
        else:
            pass
    wait client.process_commands(message)


client.run("Token")


python discord

2022-09-30 21:53

1 Answers

You can attach conditions to on_message to only certain channels.

@client.event
async def on_message(message):

@client.event
async def on_message(message):
    if message.channel.id!= Channel ID:
        return
@client.event
async def on_message(message):
    if message.channel.id not in [Channel ID, Channel ID2]:
        return


2022-09-30 21:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.