小编Kho*_*oly的帖子

使用打字稿向特定频道发送消息

每当新用户加入服务器(公会)时,我都想向“欢迎”文本频道发送问候消息。

我面临的问题是,当我找到想要的频道时,我会收到类型为 的频道GuildChannel

由于GuildChannel没有send()功能,我无法发送消息。但是我找不到找到 的方法TextChannel,所以我被困在这里。

我怎样才能到达TextChannel以便我能够使用该send()消息?在我现在使用的代码下方:

// Get the log channel (change to your liking) 
const logChannel = guild.channels.find(123456); 
if (!logChannel) return;

// A real basic message with the information we need. 
logChannel.send('Hello there!'); // Property 'send' does not exist on type 'GuildChannel'
Run Code Online (Sandbox Code Playgroud)

我正在使用 discord.js 的 11.3.0 版

node.js typescript discord.js

3
推荐指数
1
解决办法
3654
查看次数

Symfony 表单验证错误显示两次

我需要一个简单的用户注册表格。表单验证按预期工作,除了来自回调约束的验证错误由于某种原因显示两次。

我在错误消息中插入了一个随机数,以查看回调是否被调用了一次或两次。2 条错误消息显示相同的随机数,因此回调(可能)只调用一次。

任何帮助是极大的赞赏!

额外问题:“来自回调函数的所有错误都显示在表单中的同一位置(如预期)。是否(容易)指定应显示验证错误的字段?“

表单类型类如下所示:

class RegisterType extends AbstractType{

static private $em;
static private $translator;

public function __construct( EntityManager $em , TranslatorInterface $translator){
    RegisterType::$em = $em;
    RegisterType::$translator = $translator;
}

public function buildForm(FormBuilderInterface $builder , array $options){
    $builder
    ->add('email','email',[
            'attr'=>['placeholder'=>RegisterType::$translator->trans('your.email')],
            'label'=>false ,
            'constraints'=>[new NotBlank() , new Assert\Email()]])
            ->add('password','password',[
                    'attr'=>['placeholder'=>RegisterType::$translator->trans('your.password')],
                    'label'=>false ,
                    'constraints'=>[new NotBlank()]])
                    ->add('confirmPassword','password',[
                            'attr'=>['placeholder'=>RegisterType::$translator->trans('confirm.password')],
                            'label'=>false ,
                            'constraints'=>[new NotBlank()]]);
}

public function getName(){
    return 'register';
}

public function configureOptions( OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
            'constraints' =>array( new Assert\Callback( array('AppBundle\Forms\Type\RegisterType','validateForm' ) …
Run Code Online (Sandbox Code Playgroud)

php forms validation symfony

2
推荐指数
1
解决办法
2273
查看次数

为什么DateTime不能解析dd.mm.yy总是正确的?

我遇到过这个奇怪的问题:

$date1 = new DateTime('10.10.04');
$date2 = new DateTime('25.03.07');

echo $date1->format('Y-m-d'); // result: 2016-02-15
echo "<br>";
echo $date2->format('Y-m-d'); // result: 2007-03-25
Run Code Online (Sandbox Code Playgroud)

由于格式相同,我无法解释为什么第一次尝试失败,但第二次尝试成功.我能做些什么吗?

我无法更改收到日期的格式,因此我必须阅读dd.mm.yy日期格式,最后将其格式化为Ymd格式.

搜索周围有关我的东西,但我找不到任何东西.

文档中获取,这应该是行为:

而如果分隔符是破折号( - )或点(.),则假定为欧洲dmy格式.

php datetime

2
推荐指数
1
解决办法
235
查看次数

PHP获取每个preg_match的日期时间

我想从格式的字符串中获取DateTime 'YYYY-MM-DD hh:mm:ss.

要清除:我从API获取一个字符串,到现在为止字符串如下所示:

{ts '2014-09-02 14:42:49'}
Run Code Online (Sandbox Code Playgroud)

但是字符串可以改变,所以我不会只将字符串缩短到那DateTime就是为什么我想通过RegEx接收它.因为字符串可以根据需要进行更改,只要日期格式保持不变,我就会得到正确的结果.

我发现这个问题有一个解决方案,并尝试将它用于我的情况.RegEx是这样的:

$regEx = "(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})";
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

$regEx = "(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})";
Run Code Online (Sandbox Code Playgroud)

它正在regexr.com上工作,但我不能让它来处理我的PHP代码.

整个代码是这样的:

        $result = array();
        $regEx = "(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})";
        preg_match($details['date'], $regEx, $result);
        var_dump($result);
Run Code Online (Sandbox Code Playgroud)

但是var_dump提供了一个空阵列.

我如何定义RegEx以便它正在使用preg_match

php regex datetime

0
推荐指数
1
解决办法
4158
查看次数

如何重置PHPStorm?

我的问题是我无法继续我的项目,只要PHPStorm不再允许我使用组合键.像ctrl + c这样的一些关键组合仍在工作,但其他更重要的组合,如取消注释许多行,或缩进代码和其他一些不再起作用.那是从昨天开始,我的计算机在没有任何理由的情况下关闭了.

有谁知道如何重置PHPStorm的设置?我真的不想卸载并重新安装该程序.

使用:PHPStorm 9.0.2

php phpstorm

0
推荐指数
1
解决办法
6377
查看次数

检查变量是否为数字的最佳方法

我多次遇到这个问题,现在我正在考虑以下问题的最佳解决方案:

public function dumpID($id){
    var_dump($id);
}

dump("5");
Run Code Online (Sandbox Code Playgroud)

会转储string "5".数值通常作为字符串赋予函数.我经常在Symfony 2中面对这个问题,但在其他原生的PHP项目中也是如此.

在这种情况下,检查此ID是否为数字将失败

public function dumpID($id){
    if(is_int($id)){
        var_dump($id);
    } else { // throw error }
}

dump("5"); // Would fail
Run Code Online (Sandbox Code Playgroud)

所以你可以说casting to int would solve this problem.

public function dumpID($id){
    $id = (int)$id;
    if(is_int($id)){
        var_dump($id);
    } else { // throw error }
}

dump("5"); // Would var_dump the $id
Run Code Online (Sandbox Code Playgroud)

但由于PHP的以下行为,这是不正确的.

$string = "test";
$string = (int)$string;
var_dump($string); // Would give out the integer value 0
Run Code Online (Sandbox Code Playgroud)

所以

public function …
Run Code Online (Sandbox Code Playgroud)

php

-3
推荐指数
1
解决办法
544
查看次数