guys my command only working for my bot, can someone help me?
to explain better:
*fake @mybot blabla (works)
*fake @SrWonka blabla (dotn work)
Code:
Soucebin:https://sourceb.in/rkhH6YdTCx
import discord import re from discord.ext import commands class fake(commands.Cog): def _init_(self, client): self.client = client @commands.command() async def fake(self, ctx, userid: str, *, message): await ctx.message.delete() userid = re.sub('[^0-9]', '', userid) webhooks = await ctx.message.channel.webhooks() noneWH = True server = ctx.message.guild for member in server.members: if member.id == int(userid): user = member for webhook in webhooks: if(webhook.name == "boteco"): if user.nick == None: await webhook.send(content=message, username=user.name ,avatar_url=user.avatar_url) else: await webhook.send(content=message, username=user.nick ,avatar_url=user.avatar_url) noneWH = False if noneWH: webhook = await ctx.message.channel.create_webhook(name="boteco") if user.nick == None: await webhook.send(content=message, username=user.name ,avatar_url=user.avatar_url) else: await webhook.send(content=message, username=user.nick ,avatar_url=user.avatar_url) def setup(client): client.add_cog(fake(client))
Advertisement
Answer
Changed made:
The way you were getting your user is not the optimal way I used
Typing.union
you can see the docs below.display_name
it will show the nickname if there if not then it will show the username.discord.utils.get
to get the webhook instead of looping
Here is the code edited:
class fake(commands.Cog): def _init_(self, client): self.client = client @commands.command() async def fake(self, ctx, user: typing.Union[discord.Member, discord.User], *, message): # await ctx.message.delete() webhooks = await ctx.message.channel.webhooks() # get the webhook if it is there webhook = discord.utils.get(webhooks, name="boteco") if webhook: await webhook.send(content=message, username=user.display_name, avatar_url=user.avatar_url) else: webhook = await ctx.message.channel.create_webhook(name="boteco") await webhook.send(content=message, username=user.display_name ,avatar_url=user.avatar_url)
First was for another user.
Second was for the bot itself.
Docs:
A typing.Union is a special type hint that allows for the command to take in any of the specific types instead of a singular type.
Returns the user’s display name. For regular users this is just their username, but if they have a guild specific nickname then that is returned instead.
A helper that returns the first element in the iterable that meets all the traits passed in attrs. This is an alternative for
find().